package com.auto.survey.activity.monitoring.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.auto.survey.R
import com.auto.survey.activity.monitoring.entity.ctrlmeasure.ClosedLoopObservation

class CtrlMeasureAdapter : RecyclerView.Adapter<CtrlMeasureAdapter.CtrlMeasureViewHodler>() {

    private var mItems = mutableListOf<ClosedLoopObservation>()
    private var mlistener: OnItemClickListenr?=null

    fun setItems(items: List<ClosedLoopObservation>) {
        mItems.clear()
        mItems.addAll(items)
        notifyDataSetChanged()
    }

    fun setOnItemClickListener(listener:OnItemClickListenr) {
        mlistener =listener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CtrlMeasureViewHodler {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.adapter_ctrl_measure, parent, false)
        return CtrlMeasureViewHodler(view)
    }

    override fun onBindViewHolder(holder: CtrlMeasureViewHodler, position: Int) {
        holder.tvName.text = mItems[position].loopName
        holder.tvTime.text =  mItems[position].createTime
        holder.root.setOnClickListener {
            mlistener?.onItemClick(mItems[position], position)
        }
    }


    override fun getItemCount(): Int {
        if (mItems.isNotEmpty()) {
            return mItems.size
        }
        return 0
    }


    inner class CtrlMeasureViewHodler(itemview: View) : RecyclerView.ViewHolder(itemview){

        val root = itemview.findViewById<RelativeLayout>(R.id.rl_item_ctrl_measure_root)
        val tvName = itemview.findViewById<TextView>(R.id.tv_ctrl_sessionName)
        val tvTime = itemview.findViewById<TextView>(R.id.tv_ctrl_sessiontime)
    }

    interface OnItemClickListenr {
        fun onItemClick(item:ClosedLoopObservation, position: Int)
    }
}